package com.mlm.mlmapp.adapter.view;

import android.content.Context;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.androidquery.AQuery;
import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.AjaxStatus;
import com.flyco.dialog.widget.base.BottomBaseDialog;
import com.mlm.mlmapp.R;
import com.mlm.mlmapp.adapter.view.adapter.DialogItemsAdapter;
import com.mlm.mlmapp.adapter.view.listener.DialogListener;
import com.mlm.mlmapp.cons.HTTPCons;
import com.mlm.mlmapp.utils.CommonUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class AppointmentHuliDialog extends BottomBaseDialog implements
		HTTPCons {
	private Context mContext;
	private int layout;
	MyListView items;
	String id;
	List<JSONObject> list=new ArrayList<JSONObject>();
	DialogItemsAdapter adapter;
	AQuery aq;
	Button apply;
	Button cancel;
	HashMap<String, String> map = new HashMap<>();
	DialogListener l;
	String selectedID;
	public AppointmentHuliDialog(Context context, String stylistinfoId,DialogListener l,String selectedID) {
		super(context);
		// TODO Auto-generated constructor stub
		this.mContext = context;
		this.l = l;
		aq = new AQuery(context);
		map.put("stylistinfoId", stylistinfoId);
		map.put("productType", PRODUCT_TYPE_HULI);
		this.selectedID=selectedID;
	}

	@Override
	public View onCreateView() {
		// TODO Auto-generated method stub
		View inflate = View.inflate(context, R.layout.book_huli_dialog, null);
		items = (MyListView) inflate.findViewById(R.id.items);
		apply = (Button) inflate.findViewById(R.id.apply);
		cancel = (Button) inflate.findViewById(R.id.cancel);
		apply.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(-1!=adapter.selectedPosition)
					l.onApply(adapter.selectedItem,"");
				else
					l.onApply(null,"");
			}
		});
		cancel.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				l.onCancel();
			}
		});

		adapter = new DialogItemsAdapter(mContext, list);
		adapter.setSelected(selectedID);
		items.setAdapter(adapter);
		
		new Handler().postDelayed(new Runnable(){    
		    @Override
			public void run() {
		    	list.clear();
		    	map.put("productClas", PRODUCT_CLASS_HULI_LONG);
				action();		    }    
		 }, 500);
		return inflate;
	}
	

	


	private void action(){
		aq.progress(new AQProgressDialog(mContext)).ajax(CommonUtils.getURLStringParams(get_product, map),
				JSONArray.class, new AjaxCallback<JSONArray>() {

					@Override
					public void callback(String url, JSONArray array,
										 AjaxStatus status) {
						if (status.getCode() == -101) {
							Toast.makeText(aq.getContext(), "网络异常,请检查网络",
									Toast.LENGTH_SHORT);
							return;
						}
						if (array.length() == 0) {
							list.add(new JSONObject());
						}
						for (int i = 0; i < array.length(); i++) {
							try {
								list.add(array.getJSONObject(i));
							} catch (JSONException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
						adapter.notifyDataSetChanged();
					}

				});	
	}
	public void setLayout(int layout) {
		this.layout = layout;
	}

	@Override
	public boolean setUiBeforShow() {
		// TODO Auto-generated method stub
		return false;
	}

}
